package org.anderground.app.storage.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.anderground.app.R;
import org.anderground.app.storage.ConfigData.ConfigCity;
import org.anderground.core.Logger;
import org.anderground.core.storage.StorageException;

import android.content.Context;

/**
 * @author Eldar Abusalimov
 */
public class ResourceXmlStorage extends XmlStorage {

	private Context context;

	private static ResourceXmlStorage instance = null;

	/**
	 * @param context
	 * @return the instance
	 */
	public static ResourceXmlStorage getInstance(Context context) {
		if (instance == null) {
			instance = new ResourceXmlStorage(context.getApplicationContext());
		}
		if (instance.context != context.getApplicationContext()) {
			Logger.w("instance.appContext != context.getApplicationContext()");
		}
		return instance;
	}

	/**
	 * @param context
	 *            the context of the single, global Application object of the
	 *            current process
	 */
	private ResourceXmlStorage(Context context) {
		this.context = context;
	}

	@Override
	protected InputStream openConfigInputStream() throws IOException {
		return context.getResources().openRawResource(R.raw.cities_info);
	}

	@Override
	protected OutputStream openConfigOutputStream() throws IOException {
		throw new StorageException("xml writing is not implemented");
	}

	@Override
	protected InputStream openInputStreamForCity(ConfigCity city)
			throws IOException {
		throw new StorageException("not implemented");
	}

	@Override
	protected OutputStream openOutputStreamForCity(ConfigCity city)
			throws IOException {
		throw new StorageException("not implemented");
	}

}
